﻿using System;
using System.Collections.Generic;
using personalplaner.business.common;
using personalplaner.common;
using personalplaner.common.mvvm.common;
using personalplaner.dataprovider;
using personalplaner.servicemodel.common;

namespace personalplaner.servicemodel.dataaccess
{
	#region Interface

	public interface IDatabaseDataAccess
	{
		IEnumerable<string> GetOdbcList();

		IEnumerable<string> GetDatabaseList(LoginData login);

        bool TestUserConnection(LoginData login);

        bool TestDatabaseConnection(LoginData login);
	}

	#endregion

	public class DatabaseDataAccess : BaseDataAccess, IDatabaseDataAccess
	{
		public DatabaseDataAccess(ISettingsProvider settings)
            : base(settings)
        {
        }

		public IEnumerable<string> GetOdbcList()
		{
			if(Designer.IsDesignMode)
				return new List<string>();

			using (new WaitCursor())
			{
				try
				{
					return DataServices.DatabaseService.GetOdbcList();
				}
				catch (Exception)
				{
					//TODO: show a messagebox here with the error
				}

				return new List<string>();
			}
		}

		public IEnumerable<string> GetDatabaseList(LoginData login)
		{
			if (Designer.IsDesignMode)
				return new List<string>();

			using (new WaitCursor())
			{
				try
				{
					return DataServices.DatabaseService.GetDatabaseList(login);
				}
				catch (Exception)
				{
					//TODO: show a messagebox here with the error
				}

				return new List<string>();
			}
		}

        public bool TestUserConnection(LoginData login)
        {
            if (Designer.IsDesignMode)
                return false;

            using (new WaitCursor())
            {
                try
                {
                    return DataServices.DatabaseService.TestUserConnection(login);
                }
                catch (Exception)
                {
                    //TODO: show a messagebox here with the error
                }

                return false;
            }
        }

        public bool TestDatabaseConnection(LoginData login)
        {
            if (Designer.IsDesignMode)
                return false;

            using (new WaitCursor())
            {
                try
                {
                    return DataServices.DatabaseService.TestDatabaseConnection(login);
                }
                catch (Exception)
                {
                    //TODO: show a messagebox here with the error
                }

                return false;
            }
        }
	}
}
